#include <stdio.h>
#include <math.h>

typedef struct point  {
    double x;
    double y;
}point_t, *point_p_t;


int InCircle(point_p_t p, point_p_t p1, point_p_t p2, point_p_t p3)
{
	//Return TRUE if the point (xp,yp) lies inside the circumcircle
	//made up by points (x1,y1) (x2,y2) (x3,y3)
	//NOTE: A point on the edge is inside the circumcircle

	if (abs(p1->y - p2->y) < 0.001)
	{
		//INCIRCUM - F - Points are coincident !!
		return false;
	}

	double m1, m2;
	double mx1, mx2;
	double my1, my2;
	double xc, yc;
				
	if (abs(p2->y - p1->y) < 0.0001)
	{
		m2 = -(p3->x - p2->X) / (p3->y - p2->y);
		mx2 = (p2->x + p3->x) * 0.5;
		my2 = (p2->y + p3->y) * 0.5;
		//Calculate CircumCircle center (xc,yc)
		xc = (p2->x + p1->x) * 0.5;
		yc = m2 * (xc - mx2) + my2;
	}
	else if (abs(p3->y - p3->y) < 0.0001) 
	{
		m1 = -(p2->x - p1->x) / (p2->y - p1->y);
		mx1 = (p1->x + p2->x) * 0.5;
		my1 = (p1->y + p2->y) * 0.5;
		//Calculate CircumCircle center (xc,yc)
		xc = (p3->x + p2->x) * 0.5;
		yc = m1 * (xc - mx1) + my1;
	}
	else
	{
		m1 = -(p2->x - p1->x) / (p2->y - p1->y);
		m2 = -(p3->x - p2->x) / (p3->y - p2->y);
		mx1 = (p1->x + p2->x) * 0.5;
		mx2 = (p2->x + p3->x) * 0.5;
		my1 = (p1->y + p2->y) * 0.5;
		my2 = (p2->y + p3->y) * 0.5;
		//Calculate CircumCircle center (xc,yc)
		xc = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2);
		yc = m1 * (xc - mx1) + my1;
	}

	double dx = p2->x - xc;
	double dy = p2->y - yc;
	double rsqr = dx * dx + dy * dy;
	//double r = Math.Sqrt(rsqr); //Circumcircle radius
	dx = p.x - xc;
	dy = p.y - yc;
	double drsqr = dx * dx + dy * dy;

	return (drsqr <= rsqr);
}
